package com.qdlc.p2p.biz.task.impl;

import org.apache.log4j.Logger;

import com.qdlc.p2p.biz.service.AutoBorrowService;
import com.qdlc.p2p.biz.task.AbstractLoanTask;
import com.qdlc.p2p.biz.task.JobQueue;
import com.qdlc.p2p.biz.task.ValueEvent;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.util.BeanUtil;

/**
 * 标操作队列:还款，提前还款，撤回，截标等
 * @author zhangyz
 */
public class BorrowTask extends AbstractLoanTask {
    private Logger logger = Logger.getLogger(TenderTask.class);

    public BorrowTask() {
        super();
        task.setName("Borrow.Task");
    }

    @SuppressWarnings("unchecked")
    @Override
    public void doLoan() {

        JobQueue<ValueEvent> queue = JobQueue.getBorrowInstance();
        AutoBorrowService autoBorrowService = (AutoBorrowService) BeanUtil.getBean("autoBorrowService");
        while (!queue.isEmpty()) {
            ValueEvent event = queue.peek();
            if (event != null) {
                String result = "success";
                try {
                    if ("repay".equals(event.getOperate())) { // 还款
                        autoBorrowService.autoBorrowRepay(event.getBorrowRepayment());
                    } else if ("autoCancel".equals(event.getOperate())) {//管理员撤标
                        autoBorrowService.autoCancel(event.getBorrow());
                    }else if ("doPriorRepay".equals(event.getOperate())) {
                        //提前还款
                        autoBorrowService.doPriorRepay(event.getBorrowRepayment());
                    }  else if ("overdue".equals(event.getOperate())) {
                        //网站垫付
                        autoBorrowService.webAdvance(event.getBorrowRepayment());
                    }  else if ("overduePayment".equals(event.getOperate())) {
                        //前台逾期垫付
                        autoBorrowService.overduePayment(event.getBorrowRepayment());
                    }else if ("doAddBorrow".equals(event.getOperate())) {
                        //环讯发标回调处理
                        //ipsService.doAddBorrow(event.getBorrowModel());
                    }else if ("doCompensateSuccess".equals(event.getOperate())) {
                        //环讯代偿回调处理
                        autoBorrowService.autoCompensateSuccess(event.getBorrowRepayment());
                    }
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                    if (e instanceof BusinessException) {// 业务异常，保存业务处理信息
                        result = e.getMessage();
                    } else {
                        result = "系统异常，业务处理失败";
                    }
                    MailUtil.sendMail("标操作队列异常",e);
                } finally {
                    queue.remove(event);
                }
                if (event.getResultFlag() != null) {// 在需要保存系统处理信息的地方直接保存进来
                    Global.TASK_RESULT_MAP.put(event.getResultFlag(), result);
                }
            }
        }

    }

    @Override
    public Object getLock() {
        return BorrowTask.BORROW_STATUS;
    }

}
